// 查询是否为管理员
const cloud = require('wx-server-sdk');
const addUser = require("../addUser/index");
cloud.init({
  env: cloud.DYNAMIC_CURRENT_ENV
});
const db = cloud.database();
exports.main = async (event, context) => {
  if (!event.openId) {
    return {
      msg: "缺少openId",
      code: 500
    }
  }
  // 查用户表
  const res = await db.collection('user').where({
    openId: event.openId
  }).get();
  if (res.data.length) {
    return {
      data: {
        ...res.data[0]
      },
      code: 200
    }
  } else {
    try {
      const addUserRes = await addUser.main(event, context);
      const userObj = await db.collection("user").doc(addUserRes._id).get();
      return {
        data: {
          ...userObj.data
        },
        code: 200
      }
    } catch (error) {
      return {
        msg:"服务器内部异常",
        code: 500
      }
    }
  }
}